<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use think\facade\Hook;

/**
 * 视频评论管理
 * @package app\video\admin
 */
class Comment extends Admin
{
    /**
     * 首页
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
		
		 // 获取筛选
        $map = $this->getMap();
		 // 获取筛选
		$order = $this->getOrder();
        if(!$order) $order = 'create_time desc';

		//数据获取
        $data = input();
		$play_id = isset($data['id']) ? $data['id'] : 0;
		if(isset($data['_select_value'])){
			$play_id = $data['_select_value'];
		}
		if($play_id > 0){
			$father_id = model('video/VideoPlay') -> getPlayinfoByPlayid($play_id);
			if($father_id['code'] == 200 && $father_id['data']['play_type'] == 1 && $father_id['data']['father_play_id'] > 0){
				$play_id = $father_id['data']['father_play_id'];
			}
			$where = [];
			foreach($map as $value){
				if($value[0] == 'play_id'){
					$value = ['play_id', '=', $play_id];
				}
				$where[] = $value;
			}
			$map = $where;
		}

		// 数据列表
        $data_list = model('VideoPlayComment') -> withTrashed() -> where($map) -> order($order) -> paginate();
		$rel_list = [];
        foreach ($data_list as $key => $value) {
			$rel_list[$key] = $value;
			$rel_list[$key]['nick_name'] = $value['get_uc_member']['nick_name'];
			
			if($value['comment_status'] > 1){
				$value['comment_status'] = 1;
			}
        }
		//新增
		$btn_add = [
			'title' => '新增',
			'icon'  => 'fa fa-plus-circle',
			'class' => 'btn btn-primary pop',
			'href'  => url('add', ['play_id' => $play_id]),
		];
		// 评论列表
        $btn_comment = [
            'title' => '回复列表',
            'icon'  => 'fa fa-fw fa-bookmark',
            // 'class' => 'btn-default',
            'href'  => url('reply/index', ['id' => '__id__', 'play_id' => $play_id])
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('评论列表') // 设置页面标题
            ->setTableName('video_play_comment') // 设置数据表名
			->setSearch(['uid' => '用户id', 'content' => '评论详情']) // 设置搜索参数
			->addTimeFilter('create_time', '', ['开始时间', '结束时间'])
			->addTopSelect('play_id', '选择视频', model('VideoPlay') -> where(['play_operation_status' => 1, 'play_type' => 1])->column('id,main_title'), 1) // 添加筛选
            ->addColumns([ // 批量添加列
                //['__INDEX__', '序号'],
                ['id', '评论id'],
                ['uid', '用户id'],
                ['nick_name', '评论用户'],
                ['content', '评论详情'],
				['support_times', '点赞数'],
				['reply_times', '回复数'],
                ['create_time', '创建时间', 'datetime'],
                ['comment_status', '当前状态', 'status', '',[0 => '正常:success',1 => '软删除:danger']],
                ['right_button', '操作', 'btn']
            ])
			->addFilter(['uid']) // 添加筛选
			->addOrder('create_time') // 添加排序
            ->addTopButton('custom', $btn_add, true) // 批量添加顶部按钮
			->addRightButton('custom', $btn_comment, true) // 添加授权按钮
            ->addRightButtons('delete') // 批量添加右侧按钮
            // ->setPages($page) // 设置分页数据
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }
	
	public function add($play_id = ''){
		// 保存数据
        if ($this->request->isPost()) {
			$data = $this->request->post();
			$uid = $data['uid'];
			$play_id = $data['play_id'];
			$content = $data['content'];
			
			if ($uid == '' || $play_id == '' || $content == '') {
                $this->error('数据格式错误');
            }
			$rel = controller('Videologic', 'logic') -> add_comment($uid, $play_id, $content);
			if($rel['code'] == 200){
				$this->success('新增成功', null, '_parent_reload');
			}else{
				$this->error($rel['message']);
			}
		}
		
		$uid = config('background_comment_preset');
		// 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
		->setPageTitle('新增评论') // 设置页面标题
		->addSelect('uid', '选择评论用户', '系统默认角色选择',  model('app\user_center\model\user\Member') -> where('uid', 'in', $uid)->column('uid,nick_name'), '')
		->addSelect('play_id', '选择视频' , '', model('VideoPlay') -> where('play_operation_status', '=', 1)->column('id,main_title'), $play_id) // 添加筛选
		->addFormItems([ // 批量添加表单项
			['textarea', 'content', '评论详情'],
		])
		->fetch();
	}
	
    public function delete($ids = []){
        $rel = model('VideoPlayComment') -> destroy($ids, true);
        if($rel){
            $this -> success('删除成功');
        }else{
            $this -> error('删除失败');
        }
    
    }
}
